// 路由配置文件
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
export const Layout = () => import('@/layout/index.vue')
/** 固定路由 */
export const constantRoutes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: Layout,
    meta: {
      title: 'C-blog'
    },
    children: [
      {
        path: '/',
        component: () => import('@/views/home/index.vue'),
        meta: {
          title: 'C-blog - 首页'
        }
      },
      {
        path: '/category',
        component: () => import('@/views/category/index.vue'),
        meta: {
          title: 'C-blog - 分类'
        }
      },
      {
        path: '/tags',
        component: () => import('@/views/tag/index.vue'),
        meta: {
          title: 'C-blog - 标签'
        }
      },
      {
        path: '/archive',
        component: () => import('@/views/archive/index.vue'),
        meta: {
          title: 'C-blog - 归档'
        }
      },
      {
        path: '/message',
        component: () => import('@/views/message/index.vue'),
        meta: {
          title: 'C-blog - 留言板'
        }
      },
      {
        path: '/links',
        component: () => import('@/views/link/index.vue'),
        meta: {
          title: 'C-blog - 友链'
        }
      },
      {
        path: '/about',
        component: () => import('@/views/about/index.vue'),
        meta: {
          title: 'C-blog - 个人简介'
        }
      },
      {
        path: '/article/:id',
        component: () => import('@/views/article/index.vue'),
        meta: {
          title: 'C-blog - 文章详情'
        }
      },
      {
        path: '/chat',
        component: () => import('@/views/chat/index.vue'),
        meta: {
          title: 'C-blog - 聊天室'
        }
      }
      // {
      //   path: '/**',
      //   component: () => import('@/views/error/404.vue'),
      //   meta: {
      //     title: 'C-blog'
      //   }
      // }
    ]
  }
]

/** 创建路由 */
const router = createRouter({
  history: createWebHashHistory(),
  // history: createWebHistory(), // 去掉地址栏里的#
  routes: constantRoutes
})

/** 前置操作：给网页标签别名 */
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    window.document.title = to.meta.title
  }
  next()
})

export default router
